package com.example.demo.controller;

import com.example.demo.bean.AjaxRes;
import com.example.demo.service.ISysFileService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class CommonController extends BaseAdminController {

    @Resource
    private FileStorageService fileStorageService;

    @Resource
    private ISysFileService sysFileService;

    @PostMapping("/upload")
    public AjaxRes upload(@RequestParam(name = "cid", defaultValue = "0") Long cid, @RequestParam(name = "type", defaultValue = "1") Integer type, @RequestParam("file") MultipartFile file) {
        FileInfo fileInfo = fileStorageService.of(file)
                .upload();;
        return toAjax(sysFileService.addSysFile(cid, type, fileInfo));
    }
}
